using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BANSACH.BusinessLayer.DataLayer;
using BANSACH.BusinessLayer;
namespace BANSACH_GUI
{
    public partial class frmQuyenmenuList : FormBase
    {
        DataView data;
        QuyenmenuFactory objDao = new QuyenmenuFactory();
        Quyenmenu objEnity;
        QuyenmenuKeys objKeys;
        int idRowSelected = -1;

        public frmQuyenmenuList()
        {
            InitializeComponent();
        }
        private void frmQuyenmenuList_Load(object sender, EventArgs e)
        {
            CreateConfigGrid();
            // Load Data
            objDao = new QuyenmenuFactory();
            objEnity = new Quyenmenu();
            data = objDao.GetAll().DefaultView;
            //
            grvData.DataSource = data;
        }
        private void CreateConfigGrid()
        {
            grvData.ModeColorRow = 1;
            grvData.SetColorRow(System.Drawing.Color.AliceBlue);

            
				grvData.AddColumns("TextBox","maquyen","maquyen",200,true,true,"ML","");
				grvData.AddColumns("TextBox","menuma","menuma",200,true,true,"ML","");
        }
        private void SetDataFilter()
        {
            try
            {
                StringBuilder filter = new StringBuilder("1=1");                
                data.RowFilter = filter.ToString();
            }
            catch
            { }

        }
        private void btnNew_Click(object sender, EventArgs e)
        {
            frmQuyenmenuDetail frmNew = new frmQuyenmenuDetail();
            frmNew.OnAddOrEditEntity = new EventHandler(OnInserEntity);
            frmNew.ShowDialog();

        }
        private void OnInserEntity(object sender, EventArgs e)
        { 
            Quyenmenu objInserted = sender as Quyenmenu;
            if (objInserted == null) return;            
            DataRowView newrow = data.AddNew();
            // Thêm dòng vào DataView để hiển thị lên Grid
            SetEntityToDataRowView(objInserted,newrow);
        }
        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (grvData.SelectedCells.Count == 0) return;
            idRowSelected = grvData.SelectedCells[0].RowIndex;

            objKeys = new QuyenmenuKeys(int.Parse(grvData["maquyen", idRowSelected].Value.ToString()),grvData["menuma", idRowSelected].Value.ToString());
            objEnity = objDao.GetByPrimaryKey(objKeys);
            frmQuyenmenuDetail frmEdit = new frmQuyenmenuDetail(objEnity);
            frmEdit.OnAddOrEditEntity = new EventHandler(OnEditEntity);
            frmEdit.ShowDialog();
        }
        private void OnEditEntity(object sender, EventArgs e)
        {             
            DataRowView edit_Row = data[idRowSelected];
            Quyenmenu objEdited = sender as Quyenmenu;
            if (objEdited != null)
                SetEntityToDataRowView(objEdited,edit_Row);
        }
        private void SetEntityToDataRowView(Quyenmenu obj_Entity,DataRowView _row)
        {
            data.BeginInit();            
            
				 _row["maquyen"] = obj_Entity.Maquyen;
				 _row["menuma"] = obj_Entity.Menuma;
            data.EndInit();
        }
        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (grvData.SelectedCells.Count == 0) return;
            if (MessageBox.Show("Bạn chắc chắn xóa bản ghi này?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2) != DialogResult.Yes) return;
            idRowSelected = grvData.SelectedCells[0].RowIndex;

            objKeys = new QuyenmenuKeys(int.Parse(grvData["maquyen", idRowSelected].Value.ToString()),grvData["menuma", idRowSelected].Value.ToString());
            if (objDao.Delete(objKeys))
                data.Delete(idRowSelected);
        }

        private void btnFind_Click(object sender, EventArgs e)
        {
            gPanelFind.Visible = !gPanelFind.Visible;
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void grvData_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            btnEdit_Click(sender, e);
        }

        private void grvData_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                btnEdit_Click(sender, e);
            if (e.KeyCode == Keys.Delete)
                btnDelete_Click(sender, e);
        } 
      
    }
}
